mod animation;
mod physics;
mod visual;
mod mover;
mod balloon;
mod attractor;
mod animover;

use animation::Animation;
use ggez::{Context, GameResult, GameError, graphics, glam::Vec2};

pub struct State{
    movers: Vec<Box<dyn animover::Animover>>,
    attractor: attractor::Attractor
}

impl State{
    pub fn new(_ctx: &mut Context) -> State {
        let mut movers: Vec<Box<dyn animover::Animover>> = vec![];

        for i in 0..17 {
            let location = Vec2{x: (i as f32)*50.0,y: 0.0};
            let mover = balloon::Balloon::new(location);
            movers.push(Box::new(mover));

        }

        let attractor = attractor::Attractor::new(Vec2{x: 400.0, y: 300.0});
        State{ movers, attractor}
    }
}

impl ggez::event::EventHandler<GameError> for State {
    fn update(&mut self, ctx: &mut Context) -> GameResult {
        for mover in &mut self.movers {
            let fg = self.attractor.attract(mover);
            mover.apply_force(fg);

            mover.update(ctx);
        }
        self.attractor.update(ctx);
        Ok(())
    }
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::WHITE);
        self.attractor.display(ctx, &mut canvas);
        for mover in &mut self.movers {
            mover.display(ctx, &mut canvas);
        }
        canvas.finish(ctx)?;
        Ok(())
    }
}